/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.module.xmlconfig;

import java.net.URI;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import tal.drivechain.system.Version;
import tal.drivechain.wildcard.VersionElement;
import tal.drivechain.wildcard.Wildcard;

/**
 *
 * @author Main
 */
public class PackageInfo {
    private String name;
    private String description;
    private Version version;
    private URI path;
    private HashMap<String, Wildcard> dependencies = new HashMap<String, Wildcard>();

    public PackageInfo(Document packageDoc, URI uri) {
        Element modulePackage = packageDoc.getDocumentElement();
        this.name = modulePackage.getAttribute("name").replaceAll("\\s+", " ").trim();
        this.version = Version.parseVersion(modulePackage.getAttribute("version").replaceAll("\\s+", " ").trim(),
                name.toLowerCase());
        this.path = uri;
        description = "";
        NodeList children = modulePackage.getChildNodes();
        for (int i=0; i<children.getLength(); i++) {
            Node currentNode = children.item(i);
            if (currentNode.getNodeName().equals("description")) {
                description = currentNode.getTextContent().replaceAll("\\s+", " ").trim();
            }
            if (currentNode.getNodeName().equals("depends-on")) {
                NamedNodeMap attr = currentNode.getAttributes();
                Node srcNode = attr.getNamedItem("src");
                if (srcNode==null)
                    throw new IllegalArgumentException("Invalid depends-on tag: " + currentNode.toString() + " (missing src attribute).");
                String depname = currentNode.getAttributes().getNamedItem("src").getNodeValue().replaceAll("\\s+", " ").trim();
                String def = currentNode.getTextContent().replaceAll("\\s+", " ").trim();
                Wildcard wc = Wildcard.parseDefinition(def, new VersionElement());
                dependencies.put(depname, wc);
            }
        }
        
        
    }
    
    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Version getVersion() {
        return version;
    }
    
    public HashMap<String, Wildcard> getDependencies() {
        return dependencies;
    }

    public URI getURL() {
        return path;
    }
    
    
}
